const express = require('express');
const router = express.Router();
const {Setting} = require('../../models');
const { NotFoundError } = require('../../utils/errors');
const { Success, failure } = require('../../utils/responses');


//获取设置信息
router.get('/', async (req, res) => {
    try {

        const setting = await GetSetting();
        Success(res, '获取设置成功', setting);
    } catch (err) {
        failure(res, err);
    }
});

//更新设置
router.put('/', async (req, res) => {
    try {
        const setting = await GetSetting();
        await setting.update(req.body);
        Success(res, '更新成功！', setting, 200)
    } catch (err) {
        failure(res, err);
    }
});

//白名单过滤
function FilterBody(req) {
    return  {
        name: req.body.name,
        icp : req.body.icp,
        copyright:req.body.copyright
    };
}

//获取setting对象
async function GetSetting() {

    const setting = await Setting.findOne();
    if (!setting) {
        throw new NotFoundError(`初始设置未找到，请运行种子文件`);
    }
    return setting;
}

module.exports = router;